package com.xmmj.common.websocket

import com.alibaba.fastjson.JSON
import java.io.Serializable
import java.lang.Exception

/**
 * WebSocket消息
 */
class MsgBean : Serializable {
    var uid: Long = 0
    var type: String? = null
    var data: Any? = null
    var time: Long = 0

    /**
     * 将data解析成对应的对象
     *
     * @param clazz 对象类型
     */
    fun <T> parseData(clazz: Class<T>?): T? {
        if (data == null) {
            return null
        }
        try {
            return if (data is String) {
                JSON.parseObject(data as String?, clazz)
            } else {
                JSON.parseObject(JSON.toJSONString(data), clazz)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}